package com.cybertron.am.wordpress.job;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cybertron.am.wordpress.Constants;
import com.cybertron.am.wordpress.domain.CrawlQueue;
import com.cybertron.am.wordpress.persistence.CrawlQueueMapper;
import com.cybertron.am.wordpress.persistence.CrawlVisitMapper;
import com.cybertron.am.wordpress.util.SysUtil;
import com.ne.boot.common.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionDefinition;

import java.util.List;

@Component
@Slf4j
public class TaskJob {
    @Autowired
    private CrawlQueueMapper crawlQueueMapper;
    @Autowired
    private ParserAdapter parserAdapter;

    @Scheduled(fixedDelay = 10 * 1000)
    public void pull() {
        List<CrawlQueue> queues = crawlQueueMapper.selectList(new LambdaQueryWrapper<CrawlQueue>()
                .eq(CrawlQueue::getStatus, 0)
                .orderByAsc(CrawlQueue::getPriority)
                .last("LIMIT 20")
        );
        if (CollectionUtils.isEmpty(queues)) {
            log.info("无工作任务");
            return;
        }
        try {
            crawl(queues);
        } catch (Throwable e) {
            log.error("抓取数据错误", e);
        }
    }

    public void crawl(List<CrawlQueue> queues) throws Throwable {
        Constants.POOL.submit(() -> queues.parallelStream().forEach(v -> {
            try {
                parserAdapter.crawl(v);
                parserAdapter.visit(v);
            } catch (Throwable e) {
                log.error("解析错误:" + JsonUtil.writeValueAsString(v), e);
                crawlQueueMapper.updateById(CrawlQueue.builder()
                        .id(v.getId())
                        .status(-1)
                        .errorLog(SysUtil.traceError(e))
                        .build());
            }
        })).get();
    }
}
